<?php
include("sql.class.php");


class User
{

	private  $id = null;
	private $mail;
	private $password = null;
	var $nom;
	var $prenom;

	function __get($var_name)
	{
		switch( $var_name)
		{
			case 'id':	
				return $this->id;
			case 'mail':
				return $this->mail;
			case 'password':
				return $this->password;
		}
		return false;
	}
	
	function set_mail($mail)
	{
		$mail = trim($mail);
		if (!preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/' ,$mail))
			return false;
		$this->mail = $mail;
		
		//Analyse une liste d'adresses valides eu égard à la » RFC 822, comme celles trouvées dans l'en-tête To:. 
		// TODO a tester comment ça réagi si adresse n'est pas au norme de la RFC 822 ! Return false ?
		// $mail = mailparse_rfc822_parse_addresses($mail);
		// if (!is_array($mail) || $mail['isgroup'] !== false )
			// return false;
		// $this->mail = $mail['address'];
		
		return true;
	}
	
	function set_password($password1, $password2)
	{
		if ( $password1 != $password2 )
			return false;
		$this->password = sha1($password1);
		return true;
	}
	
	function verif_password($pass1) // mettre dans le code $pass1 -> post && $pass2 -> mdp de la bdd
	{	
		return ( sha1($pass1) == $this->password  );
	}
	
	function __construct( $mail = '', $password = null , $nom = '', $prenom ='')
	{
		if ( !$this->set_mail($mail))
			throw new Exception('Erreur adresse mail');
		
		$this->password = sha1($password);	
		$this->nom = $nom;
		$this->prenom = $prenom;
	}
	
	function save()
	{
		$sql = SQL::singleton();
		
		if ( $this->login=='' )
			$this->login = strstr( $this->mail , '@', true );
		
		if ($this->id === null )
		{
			$req='INSERT INTO `inscrit`
				( `id`, `login`, `mail`, `password`, `prenom`, `nom`) 
				VALUES
				(NULL, \''.$sql->escape($this->login).'\', \''.$sql->escape($this->mail).'\', 
				\''.$sql->escape($this->password).'\', \''.$sql->escape($this->prenom).'\', \''.$sql->escape($this->nom).'\' );';
			$res= $sql->query($req);
			
			//Remet à jour l'ID nouvellement généré
			$this->id = $sql->insert_id;
		}
		else
		{
			$req='UPDATE `inscrit` 
				SET `login`			= \''.$sql->escape($login).'\',
						`mail`				= \''.$sql->escape($mail).'\',
						`password`	= \''.$sql->escape($password).'\',
						`prenom`		= \''.$sql->escape($prenom).'\',
						`nom`				= \''.$sql->escape($nom).'\'
				WHERE `id` = \''.intval($id).'\'  ;';
			$res= $sql->query($req);
		}
		
	}
	
	function __toString()
	{
		return $id . ' => '.$login;
	}
	
	function delete()
	{
		$sql = SQL::singleton();
		if ($this->id !== null )
		{
			$req='DELETE FROM `inscrit` 
						WHERE `id` = \''.intval($id).'\'
						LIMIT 1;';
			$res= $sql->query($req);
		}
		$this->id = null;
	}
	
	static function get_by_id( $id )
	{
		$sql = SQL::singleton();
		
		$req='SELECT * FROM `inscrit`
			WHERE id = '.intval($id).';';
		$res= $sql->query($req);
		
		if ($res->num_rows != 1)
			return false;
		
		$sql_user = $res->fetch_assoc();
		
		$user = new User( $sql_user['mail'] );
		$user->id				= $sql_user['id'];
		$user->login			= $sql_user['login'];
		$user->password	= $sql_user['password'];
		$user->prenom		= $sql_user['prenom'];
		$user->nom			= $sql_user['nom'];
		return $user;
	}
	
	
	static function get_by_login( $login )
	{
		$sql = SQL::singleton();
		
		$req='SELECT * FROM `inscrit`
			WHERE login = \''.$sql->escape($login).'\';';
		$res= $sql->query($req);
		
		if ($res->num_rows != 1)
			return false;
		
		$sql_user = $res->fetch_assoc();
		
		$user = new User( $sql_user['mail'] );
		$user->id				= $sql_user['id'];
		$user->login			= $sql_user['login'];
		$user->password	= $sql_user['password'];
		$user->prenom		= $sql_user['prenom'];
		$user->nom			= $sql_user['nom'];
		return $user;
	}
	
}